#!/usr/bin/env python3

# Copyright (c) 2005-2014 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


class Handy:

    # FIXME: there should be a cloud database collecting all the firmware files that doesn't exist
    nonExistFirmwareList = [
        "amdgpu/aldebaran_cap.bin",
        "amdgpu/gc_11_0_0_toc.bin",
        "amdgpu/gc_11_0_3_mes.bin",
        "amdgpu/ip_discovery.bin",
        "amdgpu/navi12_cap.bin",
        "amdgpu/sienna_cichlid_cap.bin",
        "amdgpu/vega10_cap.bin",
    ] + [
        "iwlwifi-6000-6.ucode",
        "iwlwifi-Qu-b0-hr-b0-100.ucode",
        "iwlwifi-Qu-c0-hr-b0-100.ucode",
        "iwlwifi-QuZ-a0-hr-b0-100.ucode",
        "iwlwifi-br-a0-pe-a0-98.ucode",
        "iwlwifi-br-a0-petc-a0-98.ucode",
        "iwlwifi-bz-a0-fm-b0-102.ucode",
        "iwlwifi-bz-a0-fm-b0-98.ucode",
        "iwlwifi-bz-a0-fm-b0-99.ucode",
        "iwlwifi-bz-a0-fm-b0.pnvm",
        "iwlwifi-bz-a0-fm-c0-102.ucode",
        "iwlwifi-bz-a0-fm-c0-98.ucode",
        "iwlwifi-bz-a0-fm-c0-99.ucode",
        "iwlwifi-bz-a0-fm-c0.pnvm",
        "iwlwifi-bz-a0-fm4-b0-102.ucode",
        "iwlwifi-bz-a0-fm4-b0-98.ucode",
        "iwlwifi-bz-a0-fm4-b0-99.ucode",
        "iwlwifi-bz-a0-fm4-b0.pnvm",
        "iwlwifi-bz-a0-gf-a0-100.ucode",
        "iwlwifi-bz-a0-gf-a0-98.ucode",
        "iwlwifi-bz-a0-gf-a0-99.ucode",
        "iwlwifi-bz-a0-gf-a0.pnvm",
        "iwlwifi-bz-a0-gf4-a0-100.ucode",
        "iwlwifi-bz-a0-gf4-a0-98.ucode",
        "iwlwifi-bz-a0-gf4-a0-99.ucode",
        "iwlwifi-bz-a0-gf4-a0.pnvm",
        "iwlwifi-bz-a0-hr-b0-100.ucode",
        "iwlwifi-bz-a0-hr-b0-98.ucode",
        "iwlwifi-bz-a0-hr-b0-99.ucode",
        "iwlwifi-dr-a0-pe-a0-102.ucode",
        "iwlwifi-dr-a0-pe-a0-98.ucode",
        "iwlwifi-dr-a0-pe-a0-99.ucode",
        "iwlwifi-gl-b0-fm-b0-102.ucode",
        "iwlwifi-gl-b0-fm-b0-98.ucode",
        "iwlwifi-gl-b0-fm-b0-99.ucode",
        "iwlwifi-gl-b0-fm-b0.pnvm",
        "iwlwifi-gl-c0-fm-c0-102.ucode",
        "iwlwifi-gl-c0-fm-c0-99.ucode",
        "iwlwifi-ma-a0-gf-a0-100.ucode",
        "iwlwifi-ma-a0-gf-a0-89.ucode",
        "iwlwifi-ma-a0-gf-a0.pnvm",
        "iwlwifi-ma-a0-gf4-a0-100.ucode",
        "iwlwifi-ma-a0-gf4-a0-89.ucode",
        "iwlwifi-ma-a0-gf4-a0.pnvm",
        "iwlwifi-ma-a0-hr-b0-100.ucode",
        "iwlwifi-ma-a0-hr-b0-89.ucode",
        "iwlwifi-ma-b0-gf-a0-100.ucode",
        "iwlwifi-ma-b0-gf4-a0-100.ucode",
        "iwlwifi-ma-b0-hr-b0-100.ucode",
        "iwlwifi-sc-a0-fm-b0-102.ucode",
        "iwlwifi-sc-a0-fm-b0-98.ucode",
        "iwlwifi-sc-a0-fm-b0-99.ucode",
        "iwlwifi-sc-a0-fm-b0.pnvm",
        "iwlwifi-sc-a0-fm-c0-102.ucode",
        "iwlwifi-sc-a0-fm-c0-98.ucode",
        "iwlwifi-sc-a0-fm-c0-99.ucode",
        "iwlwifi-sc-a0-fm-c0.pnvm",
        "iwlwifi-sc-a0-gf-a0-100.ucode",
        "iwlwifi-sc-a0-gf-a0-98.ucode",
        "iwlwifi-sc-a0-gf-a0-99.ucode",
        "iwlwifi-sc-a0-gf-a0.pnvm",
        "iwlwifi-sc-a0-gf4-a0-100.ucode",
        "iwlwifi-sc-a0-gf4-a0-98.ucode",
        "iwlwifi-sc-a0-gf4-a0-99.ucode",
        "iwlwifi-sc-a0-gf4-a0.pnvm",
        "iwlwifi-sc-a0-hr-b0-100.ucode",
        "iwlwifi-sc-a0-hr-b0-98.ucode",
        "iwlwifi-sc-a0-hr-b0-99.ucode",
        "iwlwifi-sc-a0-wh-a0-102.ucode",
        "iwlwifi-sc-a0-wh-a0-98.ucode",
        "iwlwifi-sc-a0-wh-a0-99.ucode",
        "iwlwifi-sc-a0-wh-a0.pnvm",
        "iwlwifi-sc2-a0-fm-c0-102.ucode",
        "iwlwifi-sc2-a0-fm-c0-98.ucode",
        "iwlwifi-sc2-a0-fm-c0-99.ucode",
        "iwlwifi-sc2-a0-fm-c0.pnvm",
        "iwlwifi-sc2-a0-wh-a0-102.ucode",
        "iwlwifi-sc2-a0-wh-a0-98.ucode",
        "iwlwifi-sc2-a0-wh-a0-99.ucode",
        "iwlwifi-sc2-a0-wh-a0.pnvm",
        "iwlwifi-sc2f-a0-fm-c0-98.ucode",
        "iwlwifi-sc2f-a0-fm-c0-99.ucode",
        "iwlwifi-sc2f-a0-fm-c0.pnvm",
        "iwlwifi-sc2f-a0-wh-a0-98.ucode",
        "iwlwifi-sc2f-a0-wh-a0-99.ucode",
        "iwlwifi-sc2f-a0-wh-a0.pnvm",
        "iwlwifi-so-a0-gf-a0-100.ucode",
        "iwlwifi-so-a0-hr-b0-100.ucode",
        "iwlwifi-so-a0-jf-b0-89.ucode",
        "iwlwifi-ty-a0-gf-a0-100.ucode",
    ] + [
        "rtl_bt/rtl8723cs_cg_config.bin",
        "rtl_bt/rtl8723cs_cg_fw.bin",
        "rtl_bt/rtl8723cs_vf_config.bin",
        "rtl_bt/rtl8723cs_vf_fw.bin",
        "rtl_bt/rtl8723ds_config.bin",
        "rtl_bt/rtl8723ds_fw.bin",
        "rtl_bt/rtl8761a_config.bin",
        "rtl_bt/rtl8852bs_config.bin",
        "rtl_bt/rtl8852bs_fw.bin",
    ] + [
        "rtlwifi/rtl8723bu_bt.bin",
    ] + [
        "tehuti/aqr105-tn40xx.cld",
    ] + [
        "intel/ish/ish_*.bin",
    ]

    @staticmethod
    def isValidKernelVer(verStr):
        return True

    @staticmethod
    def compareKernelVer(verstr1, verstr2):
        """eg: 3.9.11-gentoo-r1 or 3.10.7-gentoo"""

        partList1 = verstr1.split("-")
        partList2 = verstr2.split("-")

        verList1 = [int(x) for x in partList1[0].split(".")]
        verList2 = [int(x) for x in partList2[0].split(".")]
        assert len(verList1) == 3 and all([0 <= x <= 99 for x in verList1])
        assert len(verList2) == 3 and all([0 <= x <= 99 for x in verList2])

        ver1 = verList1[0] * 10000 + verList1[1] * 100 + verList1[2]
        ver2 = verList2[0] * 10000 + verList2[1] * 100 + verList2[2]
        if ver1 > ver2:
            return 1
        elif ver1 < ver2:
            return -1

        if len(partList1) >= 2 and len(partList2) == 1:
            return 1
        elif len(partList1) == 1 and len(partList2) >= 2:
            return -1

        p1 = "-".join(partList1[1:])
        p2 = "-".join(partList2[1:])
        if p1 > p2:
            return 1
        elif p1 < p2:
            return -1

        return 0
